﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WebEditBackGround
{
    public partial class Frm_产品类别图片管理 : Form
    {
        public Frm_产品类别图片管理()
        {
            InitializeComponent();
        }

        SysClass SC = new SysClass();
        DataTable dt_当前已有产品类别 = new DataTable();
        DataTable dt_所选类别对应的图片 = new DataTable();

        WebClient myWebClient = new WebClient();

        private void Frm_产品类别图片管理_Load(object sender, EventArgs e)
        {
            try
            {
                SC.FF_DGV_格式设置(DGV_产品类别, false);
                SC.FF_DGV_格式设置(DGV_类别所属图片, false);
                FF_加载已有产品类别();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        

        private void FF_加载已有产品类别()
        {
            try
            {
                // /api/goodstype.select.all

                string cs_postUrl = SysClass.cx_url_post + "/api/goodstype.select.all";
                Code.Model.Base.RequestBase _request = new Code.Model.Base.RequestBase();

                string cs_in = JsonConvert.SerializeObject(_request);
                string cs_out = SC.FF_PostWebRequest(cs_postUrl, cs_in);

                Code.Model.ProductClass.Response_ProductClassList _response = JsonConvert.DeserializeObject<Code.Model.ProductClass.Response_ProductClassList>(cs_out);

                if (_response.TB_GoodsTypeList != null)
                {
                    dt_当前已有产品类别 = SC.FF_JsonToDataTable<Code.Model.ProductClass.TableModel_ProductClassList>(_response.TB_GoodsTypeList);
                    DGV_产品类别.DataSource = dt_当前已有产品类别;
                }
                else
                {
                    DataTable dtclear = dt_当前已有产品类别.Clone();
                    DGV_产品类别.DataSource = dtclear;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //选择想上传的图片
            try
            {

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //执行图片上传操作
            try
            {

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //停用选中的图片
            try
            {

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void DGV_产品类别_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (DGV_产品类别.SelectedRows.Count == 0)
                {
                    return;
                }

                // /api/goodstype.type.picture.select

                FF_加载选中类别对应的图片();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void FF_加载选中类别对应的图片()
        {
            string cs_postUrl = SysClass.cx_url_post + "/api/goodstype.type.picture.select";

            Code.Model.ProductPicture.Request_ProductPicture_View _request = new Code.Model.ProductPicture.Request_ProductPicture_View();
            _request.GoodsTypeID = DGV_产品类别.SelectedRows[0].Cells["GoodsTypeID"].Value.ToString();

            string cs_in = JsonConvert.SerializeObject(_request);
            string cs_out = SC.FF_PostWebRequest(cs_postUrl, cs_in);

            Code.Model.ProductPicture.Response_ProductPicture_View _response = JsonConvert.DeserializeObject<Code.Model.ProductPicture.Response_ProductPicture_View>(cs_out);

            if (_response.TB_TypePictureList != null)
            {
                dt_所选类别对应的图片 = SC.FF_JsonToDataTable<Code.Model.ProductPicture.TableModel_ProductPictureListByClass>(_response.TB_TypePictureList);
                DGV_类别所属图片.DataSource = dt_所选类别对应的图片;
            }
            else
            {
                DataTable dtclear = dt_所选类别对应的图片.Clone();
                DGV_类别所属图片.DataSource = dtclear;
            }
        }


         string css_ImgName_Old = "";
         string css_ImgRemark_Old = "";
                   

        private void DGV_类别所属图片_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (DGV_类别所属图片.SelectedRows.Count == 0)
                {
                    css_ImgName_Old = "";
                    css_ImgRemark_Old = "";
                    return;
                }

                tb_主图_图片名称.Text = DGV_类别所属图片.SelectedRows[0].Cells["ImgName"].Value.ToString();
                tb_主图_图片备注.Text = DGV_类别所属图片.SelectedRows[0].Cells["ImgRemark"].Value.ToString();

                css_ImgName_Old = tb_主图_图片名称.Text;
                css_ImgRemark_Old = tb_主图_图片备注.Text;

                try
                {
                    string picURL = SysClass.cx_path_imgsave_a + DGV_类别所属图片.SelectedRows[0].Cells["ImgPath"].Value.ToString();
                    //pictureBox主图预览.Image = Image.FromFile(picURL);
                    SC.FF_PictureBox显示URL图片(pictureBox主图预览, picURL);
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.ToString());
                    pictureBox主图预览.Image = null;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void topbut_选择图片_Click(object sender, EventArgs e)
        {
            try
            {
                string cs_fName = "";

                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.RestoreDirectory = true;
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    cs_fName = openFileDialog.FileName;
                    tb_主图_上传图片路径.Text = cs_fName;
                    pictureBox主图预览.Image = Image.FromFile(cs_fName);
                    //pictureBox新图标.SizeMode = PictureBoxSizeMode.Zoom;

                    tb_主图_图片备注.Text = "";
                    tb_主图_图片名称.Text = "";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void topbut_主图上传_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("您确认要上传一张新的图片吗？", "操作确认", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    return;
                }

                //string GalleryNum = DGV_店铺列表.SelectedRows[0].Cells["GalleryNum"].Value.ToString();
                //string GalleryName = DGV_店铺列表.SelectedRows[0].Cells["GalleryName"].Value.ToString();

                string GoodsTypeID = DGV_产品类别.SelectedRows[0].Cells["GoodsTypeID"].Value.ToString();
                string ImgName = tb_主图_图片名称.Text;
                string ImgRemark = tb_主图_图片备注.Text;

                string uriString = SysClass.cx_url_post + "/api/fileup.image.bytype?GoodsTypeID=" + GoodsTypeID + "&ImgRemark=" + ImgRemark + "&ImgName=" + ImgName;

                byte[] responseArray = myWebClient.UploadFile(uriString, "POST", tb_主图_上传图片路径.Text);

                string cs_return = System.Text.Encoding.ASCII.GetString(responseArray);

                Code.Model.Base.BaseResponse _BassResponse = JsonConvert.DeserializeObject<Code.Model.Base.BaseResponse>(cs_return);

                if (_BassResponse.ReturnValue == true)
                {
                    //MessageBox.Show(dt_OneShop_TBGalleryViewLogo.Rows[0]["ImgPath"].ToString());
                    FF_加载选中类别对应的图片();
                    //MessageBox.Show("主图上传成功！");
                }
                else
                {
                    MessageBox.Show(_BassResponse.ErrorInfo.ErrorMsg);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MessageBox.Show(ex.ToString());
            }
        }

        private void topbut_主图停用_Click(object sender, EventArgs e)
        {
            try
            {
                if (DGV_类别所属图片.SelectedRows.Count == 0)
                {
                    MessageBox.Show("没有图片可以停用");
                    return;
                }


                if (MessageBox.Show("您确认要停用选中的图片吗？", "操作确认", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    return;
                }

                string cs_ImgID = DGV_类别所属图片.SelectedRows[0].Cells["ImgID"].Value.ToString();

                // /api/goodstype.type.picture.delet

                string uriString = SysClass.cx_url_post + "/api/goodstype.type.picture.delete";
                Code.Model.ProductPicture.Request_ProductPicture_Stop _requsst = new Code.Model.ProductPicture.Request_ProductPicture_Stop();
                _requsst.ImgID = cs_ImgID;
                string cs_in = JsonConvert.SerializeObject(_requsst);
                string cs_out = SC.FF_PostWebRequest(uriString, cs_in);

                //MessageBox.Show(cs_out);
                Code.Model.Base.BaseResponse _response = JsonConvert.DeserializeObject<Code.Model.Base.BaseResponse>(cs_out);
                if (_response.ReturnValue == true)
                {

                    FF_加载选中类别对应的图片();
                    //MessageBox.Show("图片停用成功！");
                }
                else
                {
                    MessageBox.Show(_response.ErrorInfo.ErrorMsg);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MessageBox.Show(ex.ToString());
            }
        }

        private void topbut_主图信息编辑_Click(object sender, EventArgs e)
        {
            try
            {
                // /api/goodstype.type.picture.information.edit

                if (DGV_类别所属图片.SelectedRows.Count == 0)
                {
                    MessageBox.Show("没有图片可以修改");
                    return;
                }

                if (MessageBox.Show("您确认要修改选中的图片吗的信息吗？", "操作确认", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    return;
                }

                string cs_ImgID = DGV_类别所属图片.SelectedRows[0].Cells["ImgID"].Value.ToString();
                string cs_ImgName = tb_主图_图片名称.Text;
                string cs_ImgRemark = tb_主图_图片备注.Text;


                Code.Model.ProductPicture.Request_ProductPicture_Edit _request = new Code.Model.ProductPicture.Request_ProductPicture_Edit();
                _request.ImgID = cs_ImgID;
                _request.ImgName = cs_ImgName;
                _request.ImgRemark = cs_ImgRemark;
                _request.ImgName_Old = css_ImgName_Old;
                _request.ImgRemark_Old = css_ImgRemark_Old;

                string cs_in = JsonConvert.SerializeObject(_request);
                string cs_out = SC.FF_PostWebRequest(SysClass.cx_url_post + "/api/goodstype.type.picture.information.edit", cs_in);

                Code.Model.Base.BaseResponse _response = JsonConvert.DeserializeObject<Code.Model.Base.BaseResponse>(cs_out);
                if (_response.ReturnValue == true)
                {

                    FF_加载选中类别对应的图片();
                    //MessageBox.Show("图片文字数据修改成功！");
                }
                else
                {
                    MessageBox.Show(_response.ErrorInfo.ErrorMsg);
                }


                //Code.Model.Shop_one.Request_编辑展厅图片文字信息 _requset = new Code.Model.Shop_one.Request_编辑展厅图片文字信息();
                //_requset.MainID = cs_MainID;
                //_requset.ImgName = cs_ImgName;
                //_requset.ImgRemark = cs_ImgRemark;
                //_requset.ImgName_Old = css_ImgName_Old_主图;
                //_requset.ImgRemark_Old = css_ImgRemark_Old_主图;

                //string cs_in = JsonConvert.SerializeObject(_requset);
                //string cs_out = SC.FF_PostWebRequest(SysClass.cx_url_post + "/api/gallery.picture.information.edit", cs_in);
                //Code.Model.Base.BaseResponse _response = JsonConvert.DeserializeObject<Code.Model.Base.BaseResponse>(cs_out);
                //if (_response.ReturnValue == true)
                //{

                //    FF_加载一家店铺的信息();
                //    //MessageBox.Show("图片文字数据修改成功！");
                //}
                //else
                //{
                //    MessageBox.Show(_response.ErrorInfo.ErrorMsg);
                //}
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}
